﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using SPIconIntegration.Code.TimerJobs;
using SPIconIntegration.Code.Extensions;
using System.Globalization;


namespace SPIconIntegration.Code.FeatureReceivers {
    /// <summary>
    /// Represents the feature receiver for the SharePoint Icon Integration feature
    /// </summary>
    [CLSCompliant(false)]
    public class SPIconIntegrationFeatureReceiver : SPFeatureReceiver {
        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureActivated(SPFeatureReceiverProperties properties) {
            var webApp = properties.Feature.Parent as SPWebApplication;
            var icon = properties.Feature.Properties["IconExtension"].Value;
            RunJobNow(false, webApp, properties.Feature.DefinitionId, icon);
        }

        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
            var webApp = properties.Feature.Parent as SPWebApplication;
            var icon = properties.Feature.Properties["IconExtension"].Value;
            RunJobNow(true, webApp, properties.Feature.DefinitionId, icon);
        }

        /// <summary>
        /// Occurs after a Feature is installed.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureInstalled(SPFeatureReceiverProperties properties) { }

        /// <summary>
        /// Occurs when a Feature is uninstalled.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }

        /// <summary>
        /// Runs the job now.
        /// </summary>
        /// <param name="delete">if set to <c>true</c> delete the icon.</param>
        /// <param name="webApp">The web app.</param>
        /// <param name="featureId">The feature id.</param>
        /// <param name="icon">The icon.</param>
        private static void RunJobNow(bool delete, SPWebApplication webApp, Guid featureId, string icon) {
            InstallIconTimerJob newJob = new InstallIconTimerJob(webApp, featureId, delete);
            DeleteJobIfItAlreadyExists(webApp, newJob.Name);
            newJob.Schedule = new SPOneTimeSchedule(DateTime.Now);
            newJob.Title = string.Format(CultureInfo.InvariantCulture, "{0} {1} Icon.", delete ? "Delete" : "Install", icon);
            newJob.Update();
        }

        private static void DeleteJobIfItAlreadyExists(SPWebApplication webApp, string name) {
            SPJobDefinition def = webApp.GetJobDefinitionByName(name);

            if (def != null) {
                def.Delete();
            }
        }
    }
}
